package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.enterprise.models.TenderInformation;
import com.budwk.app.enterprise.services.TenderinformationService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/enterprise/enterprise/tender/information")
@ApiDefinition(tag = "招投标信息")
@Slf4j
public class TenderinformationController{
    @Inject
    private TenderinformationService  wxTenderInformationService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询招投标信息")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = TenderInformation.class
    )
    @ApiResponses(
    implementation = TenderInformation.class
    )
    @SaCheckPermission("enterprise.enterprise.wx.tender.information")
    public Result<?> list(@Param("..")TenderInformation bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getReleaseDate() !=null && Strings.isNotBlank(bean.getReleaseDate().toString())) {
            cnd.and("releaseDate","=",bean.getReleaseDate());
        }
        
        if (bean.getTitle() !=null && Strings.isNotBlank(bean.getTitle().toString())) {
            cnd.and("title","=",bean.getTitle());
        }
        
        if (bean.getEnterpriseRole() !=null && Strings.isNotBlank(bean.getEnterpriseRole().toString())) {
            cnd.and("enterpriseRole","=",bean.getEnterpriseRole());
        }
        
        if (bean.getProvinceRegion() !=null && Strings.isNotBlank(bean.getProvinceRegion().toString())) {
            cnd.and("provinceRegion","=",bean.getProvinceRegion());
        }
        
        if (bean.getInformationType() !=null && Strings.isNotBlank(bean.getInformationType().toString())) {
            cnd.and("informationType","=",bean.getInformationType());
        }
        
        if (bean.getPurchaser() !=null && Strings.isNotBlank(bean.getPurchaser().toString())) {
            cnd.and("purchaser","=",bean.getPurchaser());
        }
        
        if (bean.getSupplier() !=null && Strings.isNotBlank(bean.getSupplier().toString())) {
            cnd.and("supplier","=",bean.getSupplier());
        }
        
        if (bean.getWinningBidAmount() !=null && Strings.isNotBlank(bean.getWinningBidAmount().toString())) {
            cnd.and("winningBidAmount","=",bean.getWinningBidAmount());
        }
        
        return Result.data( wxTenderInformationService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增招投标信息")
    @ApiFormParams(
        implementation = TenderInformation.class
    )
    @ApiResponses
    @SLog(tag = "新增招投标信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.tender.information.create")
    public Result<?> create(@Param("..")TenderInformation  wxTenderInformation, HttpServletRequest req) {
         wxTenderInformation.setCreatedBy(SecurityUtil.getUserId());
         wxTenderInformationService.insert( wxTenderInformation);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改招投标信息")
    @ApiFormParams(
        implementation = TenderInformation.class
    )
    @ApiResponses
    @SLog(tag = "修改招投标信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.tender.information.update")
    public Result<?> update(@Param("..")TenderInformation  wxTenderInformation, HttpServletRequest req) {
         wxTenderInformation.setUpdatedBy(SecurityUtil.getUserId());
         wxTenderInformationService.updateIgnoreNull( wxTenderInformation);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取招投标信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.enterprise.wx.tender.information")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        TenderInformation  wxTenderInformation =  wxTenderInformationService.fetch(id);
        if ( wxTenderInformation == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data( wxTenderInformation);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除招投标信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除招投标信息", value = "${args[0]}")
    @SaCheckPermission("enterprise.enterprise.wx.tender.information.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        TenderInformation  wxTenderInformation =  wxTenderInformationService.fetch(id);
        if ( wxTenderInformation == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
         wxTenderInformationService.delete(id);
        req.setAttribute("_slog_msg",  wxTenderInformation.getId());
        return Result.success();
    }
}
